/**
 * 
 */
package org.gov.vis.data.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import org.gov.vis.components.VISHyperLink;
import org.gov.vis.data.controller.ProgencyCtlr;
import org.gov.vis.data.controller.ProgencyCtlrImpl;
import org.gov.vis.data.dto.FollowUpDetailsDto;
import org.gov.vis.data.dto.ProgencyDto;
import org.gov.vis.data.gui.AddUpdateFollowUpDetailsDialog;
import org.gov.vis.data.gui.EditInsertFollowUpDetailsDialog;
import org.gov.vis.data.gui.EditProgencyDialog;
import org.gov.vis.data.gui.EditUpdateFollowUpDetailsDialog;
import org.gov.vis.data.gui.ViewProgencyDialog;
import org.gov.vis.gui.VISDataGrid;
import org.gov.vis.main.Main;
import org.gov.vis.master.gui.listener.BaseListener;
import org.gov.vis.util.DU;
import org.gov.vis.util.Util;

/**
 * @author user
 *
 */
public class ViewProgencyListener implements ActionListener,BaseListener{


	private ProgencyCtlr progencyCtlr = new ProgencyCtlrImpl();
	private ViewProgencyDialog viewProgencyDialog;
	private ProgencyDto progencyDto;
	final private ResourceBundle rb = Util.getResources();
	private EditProgencyDialog editProgencyDialog;
	private EditUpdateFollowUpDetailsDialog editFollowUpDetailsDialog;

	public ViewProgencyListener(ViewProgencyDialog viewProgencyDialog) {
		// TODO Auto-generated constructor stub
		this.viewProgencyDialog = viewProgencyDialog;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setGuiDetailsToDto(Object _guiObject) {
		// TODO Auto-generated method stub
		ViewProgencyDialog _viewProgencyDialog =(ViewProgencyDialog)_guiObject;
		progencyDto = new ProgencyDto();

		String code = _viewProgencyDialog.getCodeTxtBx().getText();
		if(code!=null){
			progencyDto.setCode(code);
		}
	}

	@Override
	public void setDtoDetailsToGui(Object _dtoObject) {
		// TODO Auto-generated method stub
		final ProgencyDto _progencyDto = (ProgencyDto)_dtoObject;
		editProgencyDialog.getTxtFldCode().setText(_progencyDto.getCode());
		//editProgencyDialog.getTxtFldGrwBtWgt().setText(_progencyDto.getBirthWeight());
		editProgencyDialog.getTxtBxBreed().setText(_progencyDto.getBreed());
		editProgencyDialog.getTxtBxBreedType().setText(_progencyDto.getBreedType());
		editProgencyDialog.getTxtBxBullNo().setText(_progencyDto.getBullNo());
		editProgencyDialog.getDtChoCalfDateOfBirth().setDate(_progencyDto.getClfDOB());
		editProgencyDialog.getDtChoCalfDateOfBirth().setDate(_progencyDto.getClfDOB());
		editProgencyDialog.getTxtBxColor().setText(_progencyDto.getColor());
		editProgencyDialog.getTxtBxDistrict().setText(_progencyDto.getDistrict());
		editProgencyDialog.getTxtBxFarmerCode().setText(_progencyDto.getFarmerCode());
		editProgencyDialog.getTxtBxFarmerName().setText(_progencyDto.getFarmerName());
		editProgencyDialog.getTxtBxMandal().setText(_progencyDto.getMandal());
		//editProgencyDialog.getTxtFldGrwmthWgt().setText(_progencyDto.getMonthlyWeight());
		editProgencyDialog.getTxtNoOfAiDoneConc().setText(_progencyDto.getNoOfAI());
		editProgencyDialog.getTxtBxNoCalving().setText(_progencyDto.getNoOfCalving());
		editProgencyDialog.getTxtBxPercentage().setText(_progencyDto.getPercentage());
		editProgencyDialog.getTxtBxPrsntMilkYldLtrs().setText(_progencyDto.getPresentYield());
		editProgencyDialog.getTxtBxPrvVactYldLtrs().setText(_progencyDto.getPreviousYield());
		editProgencyDialog.getTxtBxRelationType().setText(_progencyDto.getRelationShipName());
		editProgencyDialog.getCombxSexType().setSelectedIndex(Integer.parseInt(_progencyDto.getSex()));
		editProgencyDialog.getTxtBxTagNo().setText(_progencyDto.getTagNo());
		editProgencyDialog.getTxtBxVillage().setText(_progencyDto.getVillage());
		editProgencyDialog.setHidnObject(_progencyDto.getPrgyId());
		editProgencyDialog.getJTextField1().setText(_progencyDto.getSirenBreed());
		editProgencyDialog.getTxtBxSiresMilkYieldLtrs().setText(_progencyDto.getDmMilkYield());
		editProgencyDialog.setFollowUpLst(_progencyDto.getFollowUpDetails());
		//System.out.println("----------------------------" + editProgencyDialog.getFollowUpLst());
		//ProgencyDto progencyDto = new ProgencyDto();
		//progencyDto.setFollowUpDetails(_progencyDto.getFollowUpDetails());

		List<String> colLst = new ArrayList<String>();
		colLst.add("Deworming");
		colLst.add("Vaccination");
		colLst.add("Deticking");
		colLst.add("Others");
		colLst.add("Nutrition");
		colLst.add("Birth Wt");
		colLst.add("Monthly Wt");
		List<FollowUpDetailsDto> followUpLst = _progencyDto.getFollowUpDetails();
		ArrayList<Object> dataList = new ArrayList<Object>(1);
		if(followUpLst != null && !followUpLst.isEmpty()){
			for(FollowUpDetailsDto dto:followUpLst){
				ArrayList<Object> lst = new ArrayList<Object>(1);
				//setting each property of the current Accessory object (row) to the list
				lst.add(dto);
				lst.add(DU.format(dto.getDeWormingDate()));
				lst.add(DU.format(dto.getVaccinationDate()));
				lst.add(DU.format(dto.getDeTickingDate()));
				lst.add(dto.getOther());
				lst.add(dto.getNutrition());
				lst.add(dto.getBirthWeight());
				lst.add(dto.getMonthlyWeight());
				dataList.add(lst);
			}//end for 
		}//end if
		VISDataGrid.createDataPanel(dataList, colLst, editProgencyDialog.getPnlFollowUpDetails(),this);

	}

	@Override
	public void clearGUILabelColors(Color _labelColor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		// TODO Auto-generated method stub


		if(actionEvent.getActionCommand().equals("Search")){
			setGuiDetailsToDto(viewProgencyDialog);
			try {

				List<ProgencyDto> aiLst = progencyCtlr.searchProgencies(progencyDto);

				List<String> colLst = new ArrayList<String>();
				colLst.add("Code");
				colLst.add("Farmer Code");
				colLst.add("Farmer Name");
				colLst.add("Bull No");


				ArrayList<Object> dataList = new ArrayList<Object>(1);
				if(aiLst != null && !aiLst.isEmpty()){
					for(ProgencyDto dto:aiLst){
						ArrayList<Object> lst = new ArrayList<Object>(1);
						//setting each property of the current Accessory object (row) to the list
						lst.add(dto);
						lst.add(dto.getCode());
						lst.add(dto.getFarmerCode());
						lst.add(dto.getFarmerName());
						lst.add(dto.getBullNo());

						dataList.add(lst);
					}//end for 
				}//end if
				VISDataGrid.createDataPanel(dataList, colLst, viewProgencyDialog.getPnlProgencyResults(),this);


			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}


		if(actionEvent.getSource() instanceof VISHyperLink){
			actionEvent.getSource();
			VISHyperLink visHyperLink = (VISHyperLink)actionEvent.getSource();
			if(visHyperLink.getObject() instanceof ProgencyDto){
				ProgencyDto progencyDto = (ProgencyDto)visHyperLink.getObject();
				editProgencyDialog = new EditProgencyDialog(Main.getMainFrame(),rb.getString("dialog_edit_prgy_title"),false);
				setDtoDetailsToGui(progencyDto);
				viewProgencyDialog.dispose();
			}
			else{

				FollowUpDetailsDto followUpDetailsDto = (FollowUpDetailsDto)visHyperLink.getObject();
				editFollowUpDetailsDialog = new EditUpdateFollowUpDetailsDialog(Main.getMainFrame(), rb.getString("dialog_edit_prmt_semen_title"), false);
				editFollowUpDetailsDialog.setProgencyObject(editProgencyDialog);
				editFollowUpDetailsDialog.setFollowUpDetailsObject(followUpDetailsDto);
				editFollowUpDetailsDialog.setTmpFollowUpLst(editProgencyDialog.getFollowUpLst());
	
				setDtoDetailsToGui1(followUpDetailsDto);

			}

			/*ProgencyDto progencyDto = (ProgencyDto)visHyperLink.getObject();
			//System.out.println("action from the object----"+aiDto.getCode());
			editProgencyDialog = new EditProgencyDialog(Main.getMainFrame(),
					rb.getString("dialog_edit_prgy_title"),
					false);
			setDtoDetailsToGui(progencyDto);

			viewProgencyDialog.dispose();*/

		}

	}

	private void setDtoDetailsToGui1(FollowUpDetailsDto followUpDetailsDto) {
		// TODO Auto-generated method stub
		//if(followUpDetDialog != null){
		editFollowUpDetailsDialog.getTxtBxOthers().setText(followUpDetailsDto.getOther());
		editFollowUpDetailsDialog.getTxtBxBirthWeight().setText(followUpDetailsDto.getBirthWeight());
		editFollowUpDetailsDialog.getTxtBxMonthlyWt().setText(followUpDetailsDto.getMonthlyWeight());
		editFollowUpDetailsDialog.getTxtBxNutrition().setText(followUpDetailsDto.getNutrition());
		editFollowUpDetailsDialog.getDtChoDateOfDeworming().setDate(followUpDetailsDto.getDeWormingDate());
		editFollowUpDetailsDialog.getDtChoDateOfDeticking().setDate(followUpDetailsDto.getDeTickingDate());
		editFollowUpDetailsDialog.getDtChoDateOfVaccination().setDate(followUpDetailsDto.getVaccinationDate());

	}


}//end class 
